using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;

public partial class Admin_Suaamthuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
          if (!IsPostBack)
        {
            LoadData();
            
        }

    }
     private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_maat .Text = id.ToString();

            tbAmthuc  athuc = tbAmthucBUS  .GetByID(id);
            txttenat.Text = athuc .Tenamthuc  ;
           
            txt_mota .Text = athuc .Mota  ;
      
            img_hmh.ImageUrl = "~/Hinhanh/Hinh/" +athuc .Hinhanh  ;
          

        }
        catch
        {
            Response.Redirect("~/Admin/Danhsachamthuc.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        int id = int.Parse(Request.QueryString["id"].ToString());
        tbAmthuc athuc = tbAmthucBUS.GetByID(id);

        athuc.Tenamthuc = txttenat.Text;



        athuc.Mota = txt_mota.Text;

        if (fu_hmh.HasFile)
        {
            string filename = DateTime.Now.Ticks.ToString() + "_" + fu_hmh.FileName;
            fu_hmh.SaveAs(Server.MapPath("~/Hinhanh/" + filename));

            System.Drawing.Image img = System.Drawing.Image.FromFile(
                Server.MapPath("~/Hinhanh/" + filename));

            ImageUtility.MakeThumbnailPhoto(
                img, 100, 100, Server.MapPath("~/Hinhanh/Hinh/"), filename);
            // xoa hinh cu
            if (athuc.Hinhanh != "no_images.jpg")
            {
                System.IO.File.Delete(Server.MapPath("~/Hinhanh/" + athuc.Hinhanh));
                System.IO.File.Delete(Server.MapPath("~/Hinhanh/Hinh/" + athuc.Hinhanh));
            }
            athuc.Hinhanh  = filename;
        }
        tbAmthucBUS.Update(athuc);
        Response.Redirect("~/Admin/Danhsachamthuc.aspx");
    }
}
